package org.wiztools.oembed;

import java.util.logging.Level;
import java.util.logging.Logger;
import static org.wiztools.oembed.OEmbedElement.*;
/**
 * A builder class for building <code>org.wiztools.oembed.OEmbedResponse</code>.
 * @author subhash
 */
public class OEmbedResponseBuilder {
    
    private static final Logger LOG = Logger.getLogger(OEmbedResponseBuilder.class.getName());
    
    private final OEmbedResponseBean bean = new OEmbedResponseBean();
    
    /**
     * Add the element.
     * @param k The key.
     * @param v The value.
     */
    public void addElement(final String k, final String v) {
        final String eName = k.trim();
        final String eValue = v.trim();

        bean.addElement(eName, eValue);
        
        switch(eName) {
            case version:
                bean.setVersion(OEmbedVersion.get(eValue));
                break;
            case type:
                bean.setType(OEmbedType.get(eValue));
                break;
            case author_url:
                bean.setAuthor_url(eValue);
                break;
            case author_name:
                bean.setAuthor_name(eValue);
                break;
            case cache_age:
                bean.setCache_age(getAsInt(eValue));
                break;
            case provider_name:
                bean.setProvider_name(eValue);
                break;
            case provider_url:
                bean.setProvider_url(eValue);
                break;
            case title:
                bean.setTitle(eValue);
                break;
            case width:
                bean.setWidth(getAsInt(eValue));
                break;
            case height:
                bean.setHeight(getAsInt(eValue));
                break;
            case url:
                bean.setUrl(eValue);
                break;
            case html:
                bean.setHtml(eValue);
                break;
            case thumbnail_url:
                bean.setThumbnail_url(eValue);
                break;
            case "thumbnail_width":
                bean.setThumbnail_width(getAsInt(eValue));
                break;
            case "thumbnail_height":
                bean.setThumbnail_height(getAsInt(eValue));
                break;
            default:
                LOG.log(Level.INFO, "Unrecognized element: <{0}>{1}</{0}>",
                        new String[]{eName, eValue});
        }
    }
    
    private static int getAsInt(String number) {
        final float f = Float.parseFloat(number);
        return Math.round(f);
    }
    
    private static String getValidationErrMessage(String field) {
        return String.format("Response not initialized: %s missing.", field);
    }
    
    /**
     * Validates and returns the built response. 
     * @return The built response.
     * @throws IllegalStateException When the response is not properly initialized.
     */
    public OEmbedResponse getResponse() throws IllegalStateException {
        // Perform validations on the response:
        if(bean.getVersion() == null) {
            throw new IllegalStateException(getValidationErrMessage("version"));
        }
        if(bean.getType() == null) {
            throw new IllegalStateException(getValidationErrMessage("type"));
        }
        if(bean.isPhoto() && bean.getUrl()==null) {
            throw new IllegalStateException(getValidationErrMessage("url"));
        }
        if((bean.isVideo() || bean.isRich()) && bean.getHtml()==null) {
            throw new IllegalStateException(getValidationErrMessage("html"));
        }
        if((bean.isPhoto() || bean.isVideo() || bean.isRich())
                && (bean.getWidth() ==0 || bean.getHeight() == 0)) {
            throw new IllegalStateException(getValidationErrMessage("width or height"));
        }
        
        // All validations have passed:
        return bean;
    }
}
